#include <chrono>
#include <string>
#include <glog/logging.h>
#include "utils.h"
#include <map>
#include <list>

using std::getenv;

namespace hello {
    static vector<string> HELLO_LIST{"Hello", "Bonjour", "Hola", "こんにちは", "Ciao", "안녕하세요"};
    static map<string, string> ANS_MAP = {
            {"你好",    "非常感谢"},
            {"Hello",   "Thank you very much"},
            {"Bonjour", "Merci beaucoup"},
            {"Hola",    "Muchas Gracias"},
            {"こんにちは",   "どうも ありがとう ございます"},
            {"Ciao",    "Mille Grazie"},
            {"안녕하세요",   "대단히 감사합니다"}
    };

    string Utils::hello(int index) {
        return HELLO_LIST[index];
    }

    string Utils::thanks(string key) {
        return ANS_MAP[key];
    }

    list<TalkRequest> Utils::buildLinkRequests() {
        list<TalkRequest> requests = {};
        TalkRequest talkRequest;
        for (int i = 0; i < 3; ++i) {
            string data = grpc::to_string(random(5));
            //std::to_string
            talkRequest.set_data(data);
            talkRequest.set_meta("C++");
            requests.push_front(talkRequest);
        }
        return requests;
    }

    int Utils::random(int n) {
        int a = 0;
        int b = n;
        return (rand() % (b - a + 1)) + a;
    }

    long Utils::now() {
        const auto now = chrono::system_clock::now();
        auto value = now.time_since_epoch().count();
        return value;
    }

    string Utils::getServerHost() {
        const char *server_address = getenv("GRPC_SERVER");
        string endpoint(server_address ? server_address : "localhost");
        return endpoint;
    }

    string Utils::getServerPort() {
        const char *serverPort = getenv("GRPC_SERVER_PORT");
        string port(serverPort ? serverPort : "9996");
        return port;
    }

    string Utils::getBackendPort() {
        const char *port = getenv("GRPC_HELLO_BACKEND_PORT");
        string backendPort(port ? port : "");
        if (backendPort.empty()) {
            return getServerPort();
        }
        return backendPort;
    }

    string Utils::getBackend() {
        const char *server_address = getenv("GRPC_HELLO_BACKEND");
        string endpoint(server_address ? server_address : "");
        if (endpoint.empty()) {
            return getServerHost();
        }
        return endpoint;
    }

    string Utils::getSecure() {
        const char *isTls = getenv("GRPC_HELLO_SECURE");
        string secure(isTls ? isTls : "");
        return secure;
    }

    void Utils::initLog(char *const *argv) {
        /*
         * 日志文件名 <program name>.<host name>.<user name>.log.<Severity level>.<date>-<time>.<pid>
         * 日志格式 [IWEF]yyyymmdd hh:mm:ss.uuuuuu threadid file:line] msg
         */
        google::InitGoogleLogging(argv[0]);
        /*
         * sudo mkdir /opt/hello-grpc
         * sudo chown -R $(whoami) /opt/hello-grpc
         */
        google::SetLogDestination(google::INFO, "/opt/hello-grpc/");
        FLAGS_colorlogtostderr = true;
        FLAGS_alsologtostderr = true;
    }
}